//
//  extension.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/22.
//

import Foundation
import UIKit

extension UIColor {
    
//    class func colorConvertFromString(rgbValue:Int) -> UIColor {
//           return UIColor.init(_colorLiteralRed:((Float)((rgbValue & 0xFF0000) >> 16))/255.0, green: ((Float)((rgbValue & 0xFF00) >> 8))/255.0, blue: ((Float)(rgbValue & 0xFF))/255.0 , alpha: 1.0)
//       }
    
    convenience init(hexString: String) {
        let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: hexString)
             
        if hexString.hasPrefix("#") {
            scanner.scanLocation = 1
        }
             
        var color: UInt32 = 0
        scanner.scanHexInt32(&color)
             
        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask
             
        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0
             
        self.init(red: red, green: green, blue: blue, alpha: 1)
    }
         
    // UIColor -> Hex String
    var hexString: String? {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
             
        let multiplier = CGFloat(255.999999)
             
        guard self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) else {
            return nil
        }
             
        if alpha == 1.0 {
            return String(
                format: "#%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier)
            )
        } else {
            return String(
                format: "#%02lX%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier),
                Int(alpha * multiplier)
            )
        }
    }
}

extension UITableView {
    func tableViewDisplayMessage(message:String,rowCount:Int) {
        if rowCount == 0 {
            let backView:UIView = UIView()
            self.backgroundView = backView
            let backImgView:UIImageView = UIImageView.init(frame: CGRect.init(x: backView.frame.midX - 77, y: backView.frame.height/2 - 62 - 50, width: 154, height: 124))
            backImgView.image = UIImage.init(named: "largeAmountQuery_nothingPrompt_Icon")
            backView.addSubview(backImgView)
            
            let backLabel:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: backImgView.frame.maxY, width: backView.frame.size.width, height: 60))
            backLabel.numberOfLines = 0
            backLabel.text = message
            backLabel.font = UIFont.systemFont(ofSize: 19)
            backLabel.textColor = UIColor.gray
            backLabel.textAlignment = NSTextAlignment.center
            backView.addSubview(backLabel)
        } else {
            self.backgroundView = nil
        }
    }
    

}

/**
 ** lineLength:     虚线的宽度
 ** lineSpacing:    虚线的间距
 ** lineColor:      虚线的颜色
 **/
extension UIView {
    func drawDashLine(lineLength:Int,lineSpacing:Int,lineColor:UIColor,isLevel:Bool) {
        let shapeLayer = CAShapeLayer.init(layer: layer)
        shapeLayer.bounds = self.bounds
        if isLevel {
            shapeLayer.position = CGPoint.init(x: self.frame.width/2, y: self.frame.height/2)
        } else {
            shapeLayer.position = CGPoint.init(x: self.frame.width/2, y: self.frame.height/2)
        }
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = lineColor.cgColor
        if isLevel {
            shapeLayer.lineWidth = self.frame.height
        } else {
            shapeLayer.lineWidth = self.frame.width
        }
        shapeLayer.lineJoin = .round
        shapeLayer.lineDashPattern = Array.init(arrayLiteral: NSNumber.init(value: lineLength),NSNumber.init(value: lineSpacing))
        let path = CGMutablePath.init()
        path.move(to: CGPoint.init(x: 0, y: 0),transform: .identity)
        if isLevel {
            path.addLine(to: CGPoint.init(x: self.frame.width, y: 0),transform: .identity)
        } else {
            path.addLine(to: CGPoint.init(x: 0, y: self.frame.height),transform: .identity)
        }
        shapeLayer.path = path
        self.layer.addSublayer(shapeLayer)
    }
}

extension UILabel {
    func changeLineSpaceForLabel(space:CGFloat) {
        let attributedString = NSMutableAttributedString.init(string: self.text ?? "")
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = space
        attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, self.text?.count ?? 0))
        self.attributedText = attributedString
        self.sizeToFit()
    }
}
